package com.lekang.le.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import com.lekang.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lekang.le.mapper.BillMapper;
import com.lekang.le.domain.Bill;
import com.lekang.le.service.IBillService;

/**
 * 账单Service业务层处理
 * 
 * @author caoyangyang
 * @date 2025-10-27
 */
@Service
public class BillServiceImpl implements IBillService 
{
    @Autowired
    private BillMapper billMapper;

    /**
     * 查询账单
     * 
     * @param id 账单主键
     * @return 账单
     */
    @Override
    public Bill selectBillById(Long id)
    {
        return billMapper.selectBillById(id);
    }

    /**
     * 查询账单列表
     * 
     * @param bill 账单
     * @return 账单
     */
    @Override
    public List<Bill> selectBillList(Bill bill)
    {
        return billMapper.selectBillList(bill);
    }

    /**
     * 新增账单
     * 
     * @param bill 账单
     * @return 结果
     */
    @Override
    public int insertBill(Bill bill)
    {
        bill.setCreateTime(DateUtils.getNowDate());
        return billMapper.insertBill(bill);
    }

    /**
     * 修改账单
     * 
     * @param bill 账单
     * @return 结果
     */
    @Override
    public int updateBill(Bill bill)
    {
        return billMapper.updateBill(bill);
    }

    /**
     * 批量删除账单
     * 
     * @param ids 需要删除的账单主键
     * @return 结果
     */
    @Override
    public int deleteBillByIds(Long[] ids)
    {
        return billMapper.deleteBillByIds(ids);
    }

    /**
     * 删除账单信息
     * 
     * @param id 账单主键
     * @return 结果
     */
    @Override
    public int deleteBillById(Long id)
    {
        return billMapper.deleteBillById(id);
    }

    /**
     * 创建订单支付账单
     * 
     * @param orderNo 订单编号
     * @param elderlyId 老人ID
     * @param amount 订单金额
     * @param prepaymentAmount 从预交款扣除的金额
     * @param depositAmount 从押金扣除的金额
     * @return 账单对象
     */
    @Override
    public Bill createOrderBill(String orderNo, Long elderlyId, BigDecimal amount, 
                                BigDecimal prepaymentAmount, BigDecimal depositAmount)
    {
        // 生成账单编号（规则：BILL + yyyyMMddHHmmss + 随机4位）
        String billNo = generateBillNo();
        
        // 创建账单对象
        Bill bill = new Bill();
        bill.setBillNo(billNo);
        bill.setBillType("服务订单");
        bill.setElderlyId(elderlyId);
        bill.setBillAmount(amount);
        bill.setPayableAmount(amount);
        bill.setPrepaymentAmount(prepaymentAmount);
        bill.setTransactionStatus("已支付"); // 账单表使用文字状态
        bill.setRelatedOrder(orderNo);
        bill.setPaymentChannel("余额支付");
        bill.setPaymentMethod("账户余额");
        bill.setCreateTime(DateUtils.getNowDate());
        
        // 如果从押金扣款，在备注中说明
        if (depositAmount != null && depositAmount.compareTo(BigDecimal.ZERO) > 0) {
            bill.setPaymentRemark("预交款支付：" + prepaymentAmount + "元，押金支付：" + depositAmount + "元");
        } else {
            bill.setPaymentRemark("预交款支付：" + prepaymentAmount + "元");
        }
        
        // 插入账单
        billMapper.insertBill(bill);
        
        return bill;
    }

    /**
     * 生成账单编号
     * 规则：BILL + yyyyMMddHHmmss + 随机4位数字
     * 
     * @return 账单编号
     */
    private String generateBillNo() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String datePart = sdf.format(new Date());
        
        // 生成4位随机数字
        int random = (int) ((Math.random() * 9000) + 1000);
        
        return "BILL" + datePart + random;
    }
}
